import request from '@/utils/request'

export function uploadImage(file) {
    const formData = new FormData()
    formData.append('image_upload', file)
    return request({
        url: '/news/upload',
        method: 'POST',
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        data: formData
    })
}

// 新建文章
export function createPost(data) {
    return request({
        url: '/news',
        method: 'POST',
        data
    })
}

// 更新文章
export function updatePost(id, data) {
    // TODO:
    console.log(data)
    return request({
        url: '/news/' + id,
        method: 'PUT',
        data
    })
}

// 获取文章详情
export function getPostDetail(id) {
    return request(({
        url: '/news/' + id,
        method: 'GET'
    }))
}

// 获取文章列表
export function getPostList(params) {
    return request({
        url: '/news/condition',
        method: 'GET',
        params
    }).then(data => {
        return data.page_res
    })
}

// 改变文章状态
export function switchPostStatus(id) {
    return request({
        url: '/news/' + id + '/invisible',
        method: 'PATCH'
    })
}

// 删除文章
export function deletePost(id) {
    return request({
        url: '/news/' + id,
        method: 'DELETE'
    })
}

// 改变文章顺序
export function changePostOrder(id, order) {
    return request({
        url: '/news/' + id + '/order',
        method: 'PATCH',
        params: { order }
    })
}
